package com.plate.modules.system.controller;

import com.plate.common.core.result.Result;
import com.plate.common.core.utils.StrUtils;
import com.plate.modules.system.domain.po.SysFile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDate;

/**
 * @ClassName SysFileController
 * @Description TODO
 * @Author jiaxd
 * @Date 2023年11月25日 10:38
 * @Version 1.0
 */

@RestController
@RequestMapping("/sys/sysFile")
public class SysFileController {

    @Value("${uploadFile.base.dir}")
    private String ctxPath;

    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam("file") MultipartFile multipartFile) {

        // 文件原名
       try {

           String filePath = ctxPath + File.separator + LocalDate.now().getYear() + File.separator + LocalDate.now().getMonthValue() + File.separator + LocalDate.now().getDayOfMonth();
           File file = new File(filePath);
           if (!file.exists()) {
               // 创建文件根目录
               file.mkdirs();
           }
           String orgName = multipartFile.getOriginalFilename();
           String fileName = StrUtils.getRandomNumberString(16);
           String ext = orgName.substring(orgName.lastIndexOf(".") + 1).toLowerCase();
           multipartFile.transferTo(new File(filePath + File.separator + fileName + "." + ext));
           SysFile sysFile = new SysFile();
           sysFile.setFileName(fileName);
           sysFile.setOrigName(orgName);
           sysFile.setFilePath(filePath);
           sysFile.setSuffix(ext);
           sysFile.setSize(multipartFile.getSize());
           sysFile.setVersion("1");
       } catch (Exception e) {
           e.printStackTrace();
       }

        return Result.SUCCESS();
    }


//    public Result downloadFile(HttpServletRequest request, HttpServletResponse response) {
//
//    }


}
